package com.shenmazong.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.shenmazong.config.OssUploadTemplate;
import com.shenmazong.entity.TbCodeing;
import com.shenmazong.mapper.TbCodeingMapper;
import com.shenmazong.service.TbCodeingService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.shenmazong.utils.ResultResponse;
import com.shenmazong.vo.CodeingVo;
import com.shenmazong.vo.ListParamVo;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;

/**
 * <p>
 * 大纲教学代码  ——>  John
 服务实现类
 * </p>
 *
 * @author 还怕大雨吗
 * @since 2021-09-18
 */
@Service
public class TbCodeingServiceImpl extends ServiceImpl<TbCodeingMapper, TbCodeing> implements TbCodeingService {

    @Resource
    private TbCodeingMapper tbCodeingMapper;

    @Resource
    private OssUploadTemplate ossUploadTemplate;


    /***
     * @description 查询全部代码块
     * @params [listParamVo]
     * @return com.shenmazong.utils.ResultResponse
     * @author -还怕大雨吗
     * @date 2021/9/18 13:22
     */
    @Override
    @Cacheable(value = "selectCodeIng")
    public ResultResponse selectCodeIng(ListParamVo listParamVo) {
        IPage<CodeingVo> page = tbCodeingMapper.selectCodeIng(
                new Page<CodeingVo>(listParamVo.getCurrent(),listParamVo.getSize()),
                  listParamVo.getParamMap().get("teach")
        );
        return ResultResponse.SUCCESS(page);
    }


    /***
     * @description 新增代码块
     * @params [listParamVo]
     * @return com.shenmazong.utils.ResultResponse
     * @author -还怕大雨吗
     * @date 2021/9/18 13:23
     */
    @Override
    @CacheEvict(value = "selectCodeIng",allEntries = true)
    public ResultResponse insertCodeIng(TbCodeing listParamVo) {
        tbCodeingMapper.insert(listParamVo);
        return ResultResponse.SUCCESS();
    }

    /***
     * @description 修改代码快
     * @params [listParamVo]
     * @return com.shenmazong.utils.ResultResponse
     * @author -还怕大雨吗
     * @date 2021/9/18 13:23
     */
    @Override
    @CacheEvict(value = "selectCodeIng",allEntries = true)
    public ResultResponse updateCodeIng(TbCodeing listParamVo) {
        tbCodeingMapper.updateById(listParamVo);
        return ResultResponse.SUCCESS();
    }

    /***
     * @description 删除代码块
     * @params [listParamVo]
     * @return com.shenmazong.utils.ResultResponse
     * @author -还怕大雨吗
     * @date 2021/9/18 13:23
     */
    @Override
    @CacheEvict(value = "selectCodeIng",allEntries = true)
    public ResultResponse deleteCodeIng(Long listParamVo) {
        tbCodeingMapper.deleteById(listParamVo);
        return ResultResponse.SUCCESS();
    }

    /***
     * @description 上传文件
     * @params [file]
     * @return com.shenmazong.utils.ResultResponse
     * @author -还怕大雨吗
     * @date 2021/9/24 9:36
     */
    @Override
    public ResultResponse uploadSescc(MultipartFile file) {
        return ossUploadTemplate.uploadFileObjectStorage(file);
    }

    /***
     * @description 下载文件
     * @params [filename]
     * @return com.shenmazong.utils.ResultResponse
     * @author -还怕大雨吗
     * @date 2021/9/24 20:08
     */
    @Override
    public ResultResponse downlodSescc(String filename) {
        return  ossUploadTemplate.downloadObjectStorage(filename);
    }
}
